//
//  BHDialogView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/31.
//

import UIKit

protocol BHDialogViewDelegate {
    func didDialogSelectIndex(type: Int)
}

class BHDialogView: UIView {
    @IBOutlet weak var shareBtn: UIButton!
    
    @IBOutlet weak var reportBtn: UIButton!
    
    @IBOutlet weak var pop_top: NSLayoutConstraint!
    
    @IBOutlet weak var containtImageView: UIImageView!
    
    @IBOutlet weak var pop_traling: NSLayoutConstraint!
    
    var delegate: BHDialogViewDelegate?
     
    
    // 默认0 分享和举报 1分享和删除
    var popType: Int = 0 {
        didSet {
            if popType == 1 {
                self.reportBtn.setTitle("   删   除", for: .normal)
                self.reportBtn.setImage(UIImage.init(named: "bh_circle_chosePlace_del"), for: .normal)
            }
        }
    }
    
    
    static func loadFromXib() -> BHDialogView {
        let viewArray = Bundle.main.loadNibNamed("BHDialogView", owner: nil, options: nil)
        let view = viewArray![0] as! BHDialogView
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height)
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.isHidden = true
        self.backgroundColor = UIColor.white.withAlphaComponent(0.0)
        self.pop_top.constant = UIScreen.begainY
        
        
    }
    
    @IBAction func shareAcr(_ sender: UIButton) {
        self.delegate?.didDialogSelectIndex(type: 0)
        self.isHidden = true
    }
    
    
    @IBAction func reportAct(_ sender: UIButton) {
        self.delegate?.didDialogSelectIndex(type: 1)
        self.isHidden = true
    }
    
    
    func show() {
        self.superview?.bringSubviewToFront(self)
        self.isHidden = false
    }
    
    func hide() {
        self.isHidden = true
    }
    
    ///点击任意位置view消失
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        let currentPoint = touches.first?.location(in: self)
        if !self.containtImageView.frame.contains(currentPoint ?? CGPoint()) {
            self.hide()
        }
    }
}
